{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['./testall.txt']\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import re\n",
    "import sys\n",
    "import codecs\n",
    "import datetime\n",
    "\n",
    "def load_files(files):\n",
    "\t'''\n",
    "\t:param files:文件列表\n",
    "\t:return:文件内容\n",
    "\t'''\n",
    "\ttext = []\n",
    "\tprint(files)\n",
    "\tfor file in files:\n",
    "\t\tif file:\n",
    "\t\t\twith codecs.open(file, 'rb', encoding='utf-8') as f:\n",
    "\t\t\t\ttext.extend(f.readlines())\n",
    "\treturn text\n",
    "text = load_files(['./testall.txt'])\n",
    "regex=re.compile('.+/nr')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['19980101-01-001-002/m', '中共中央/nt', '总书记/n', '、/w', '国家/n', '主席/n', '江/nr', '泽民/nr\\n']\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "sent=text[1]\n",
    "tokens=sent.split('  ')\n",
    "print(tokens)\n",
    "print(len(tokens))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 6 ['江/nr']\n",
      "7 -1 []\n"
     ]
    }
   ],
   "source": [
    "groups=[]\n",
    "first_index=-1\n",
    "last_index=-1\n",
    "i=0\n",
    "j=0\n",
    "while i<len(tokens):\n",
    "    t=tokens[i]\n",
    "    if regex.match(t):\n",
    "        if first_index==-1:#找到块的第一个时间\n",
    "            first_index=i\n",
    "            j=0\n",
    "            while j<len(tokens[i+1:]):\n",
    "                tt=tokens[i+1+j]\n",
    "                if regex.match(tt):\n",
    "                    last_index=i+1+j#找到块的最后一个时间\n",
    "                    j+=1\n",
    "                else:\n",
    "                    last_index=i+j#找到块的最后一个时间\n",
    "                    break\n",
    "            print(first_index,last_index,tokens[first_index:last_index+1])\n",
    "            groups.append((first_index,last_index))\n",
    "            if last_index==-1:\n",
    "                break\n",
    "            i=last_index\n",
    "            first_index=-1\n",
    "            last_index=-1\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['泽民/nr\\n']"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokens[6+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for line in text:\n",
    "    tokens=line.split(' ')\n",
    "\n",
    "    groups=[]\n",
    "    first_index=-1\n",
    "    last_index=-1\n",
    "    i=0\n",
    "    j=0\n",
    "    while i<len(tokens):\n",
    "        t=tokens[i]\n",
    "        if regex.match(t):\n",
    "            if first_index==-1:#找到块的第一个时间\n",
    "                first_index=i\n",
    "                j=0\n",
    "                while j<len(tokens[i+1:]):\n",
    "                    tt=tokens[i+1+j]\n",
    "                    #if regex.match(tt) or ''==tt.strip():#解决相邻时间有多个空格的时候无法将相邻时间分在一个块的问题，其实是语料本身的问题\n",
    "                    if regex.match(tt):\n",
    "                        pass\n",
    "                    else:\n",
    "                        last_index=i+1+j#找到块的最后一个时间\n",
    "                        break\n",
    "                    j+=1\n",
    "                print(first_index,last_index,tokens[first_index:last_index])\n",
    "                groups.append((first_index,last_index))\n",
    "                i=last_index\n",
    "                first_index=-1\n",
    "                last_index=-1\n",
    "        i+=1\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
